// Purpose : This page show the Blog posted by a doctor or dentist
// Author  : Vinayak Singh Rathore
// Date    : 24 Sep 2007
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;

public partial class Community_BelogDetail : System.Web.UI.Page
{
    protected string DoctorName = "";
    protected bool monthid = false;
    protected bool bid = false;
    public int strBlogId = 0;
    public int strBlogType = 0;
    public string strURlDoctorname = string.Empty;
    public string strURlBlogName = string.Empty;
    public string strURL = string.Empty;
    public string Message = string.Empty;
    protected localFunction lfunction = new localFunction();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //COMMENTED BY RANJEET AT 21/02/2008 TO VIEW MULTIPLE BLOG OF DOCTORS/DENTIST
            //SetBelog();
            //END 

            //Added By ranjeet AT 21/02/2008
            // This function use to redirect at community/community.aspx when anyone access page with wrong/change  URL
            preventErrorWhenUrlChangesByUser();
            try
            {
                if (bid == false)
                {
                    //THESE SESSION WILL USE WHEN WE SEE BLOG OF DOCTOR/DENTIST BY ARCHIVE .
                    Session["BlogID"] = null;
                    Session["BlogType"] = null;
                    Session["BlogID"] = Convert.ToInt32(Request.QueryString["bid"]);
                    strBlogId = Convert.ToInt32(Request.QueryString["bid"]);
                    Session["BlogType"] = Convert.ToInt32(Request.QueryString["type"]);
                    strBlogType = Convert.ToInt32(Request.QueryString["type"]);
                }
                BindGrid(Convert.ToInt32(Session["BlogID"]));
            }
            catch
            {
                Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
            }
        }


    }
    /// <summary>
    /// This function return blog message posted by doctor or dentist
    /// </summary>
    /// 
    //COMMENTED AT 21/02/2008 BY RANJEET

    //public void SetBelog()
    //{
    //    Manager objManger = new Manager(true,"ConnectionString");
    //    SqlDataReader objDataReader = null;
    //    objDataReader = objManger.ExecuteQuery("select blogMessage from blog where doctorID=" + Convert.ToInt32(Request.QueryString["bid"]));
    //    if (objDataReader.HasRows)
    //    {
    //        objDataReader.Read();
    //        lblBlog.Text = Server.HtmlDecode(objDataReader["blogMessage"].ToString());
    //    }
    //    objDataReader.Close();
    //    objDataReader = null;

    //    int type = Convert.ToInt32(Request.QueryString["type"]);
    //    if (type == 2)
    //    {
    //        objDataReader = objManger.ExecuteQuery("select fname from tbl_doctors where id=" + Convert.ToInt32(Request.QueryString["bid"]));
    //    }
    //    else
    //    {
    //        objDataReader = objManger.ExecuteQuery("select fname from tbl_dentists where id==" + Convert.ToInt32(Request.QueryString["bid"]));
    //    }

    //    if (objDataReader.HasRows)
    //    {
    //        objDataReader.Read();
    //        DoctorName = objDataReader["fname"].ToString();
    //    }

    //    objDataReader.Close();
    //    objDataReader = null;
    //    objManger = null;
    //}

    //END COMMENTED BLOCK

    private void BindGrid(int id)
    {

        //Filling the grid with the messages of the topic selected in the combo
        Manager mgrobj = new Manager(true,"ConnectionString");
        string sql = "";

        if (Convert.ToInt32(Session["BlogType"]) == 2)
        {
            if (Request.QueryString["monthid"] != null)
            {

                sql = "select substring(blog.blogMessage,1,500)as BlogMessage ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,doc.FName+' '+doc.MName+' '+doc.Lname ";
                sql = sql + "as Name,blog.blogPostedDate,blog.id as blogid   from blog inner join tbl_doctors doc on  blog.doctorID=doc.id ";
                sql = sql + " where blog.doctorID=" + id + "   and month(blogPostedDate) =" + Convert.ToInt32(Request.QueryString["monthid"]) + " and year(blogPostedDate)=year(getdate())";

            }
            else
            {
                sql = "select substring(blog.blogMessage,1,500) as BlogMessage ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,doc.FName+' '+doc.MName+' '+doc.Lname ";
                sql = sql + "as Name,blog.blogPostedDate ,blog.id as blogid  from blog inner join tbl_doctors doc on  blog.doctorID=doc.id ";
                sql = sql + " where blog.doctorID=" + id + " order by blogposteddate desc";

                //Response.Write(sql);
            }
        }
        else
        {
            if (Request.QueryString["monthid"] != null)
            {
                sql = "select substring(blog.blogMessage,1,500) as BlogMessage ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,dent.FName+' '+dent.MName+' '+dent.Lname  ";
                sql = sql + "as Name,blog.blogPostedDate,blog.id as blogid from blog inner join tbl_dentists dent on  blog.doctorID=dent.id ";
                sql = sql + " where blog.doctorID=" + id + " and month(blogPostedDate) =" + Convert.ToInt32(Request.QueryString["monthid"]) + " and year(blogPostedDate)=year(getdate())";
            }
            else
            {
                sql = "select substring(blog.blogMessage,1,500) as BlogMessage ,blog.blogTitle as BlogTitle,REPLACE(LTRIM(RTRIM(blog.blogTitle)), ' ', '_') as BlogTitleURL,dent.FName+' '+dent.MName+' '+dent.Lname  ";
                sql = sql + "as Name,blog.blogPostedDate,blog.id as blogid  from blog inner join tbl_dentists dent on  blog.doctorID=dent.id ";
                sql = sql + " where blog.doctorID=" + id + " order by blogposteddate desc";
            }
        }

        if (sql.ToString() != "")
        {
            DataSet ds = mgrobj.ExecuteQuery(sql, "tbl");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DoctorName = ds.Tables[0].Rows[0]["Name"].ToString();
                strURlBlogName = ds.Tables[0].Rows[0]["BlogTitle"].ToString();
                strURlDoctorname = ds.Tables[0].Rows[0]["Name"].ToString();
                strURlDoctorname = strURlDoctorname.ToString().Trim().Replace(" ", "_");
                strURlBlogName = strURlBlogName.ToString().Trim().Replace(" ", "-");
                strURlBlogName = strURlBlogName.ToString().Trim().Replace(".", "-");
                Message = ds.Tables[0].Rows[0]["blogid"].ToString();
                //strURL = Session["strHttp"] + "blog/" + strURlDoctorname;//+ "/" +Message + "_" + strURlBlogName + ".aspx";
                strURL = Session["strHttp"] + "blog";
                grdMessages.DataSource = ds.Tables[0];
                grdBlogTitle.DataSource = ds.Tables[0];
                //grdMessages.PageSize = Convert.ToInt32(Session["AdminPageSize"]);
                grdMessages.DataBind();
                grdBlogTitle.DataBind();
            }
            else // no topics exist
            {
                grdMessages.DataSource = ds.Tables[0];
                grdBlogTitle.DataSource = ds.Tables[0];
                //grdMessages.PageSize = Convert.ToInt32(Session["AdminPageSize"]);
                grdMessages.DataBind();
                grdBlogTitle.DataBind();
                return;
            }
            mgrobj = null;
        }
    }
    protected void grdMessages_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdMessages.PageIndex = e.NewPageIndex;
        //BindGrid(Convert.ToInt32(Request.QueryString["bid"]));

        grdBlogTitle.PageIndex = e.NewPageIndex;
        BindGrid(Convert.ToInt32(Request.QueryString["bid"]));
    }

    protected void grdBlogTitle_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdBlogTitle.PageIndex = e.NewPageIndex;
        grdMessages.PageIndex = e.NewPageIndex;

        BindGrid(Convert.ToInt32(Request.QueryString["bid"]));
    }

    //private void BindGrid()
    //{
    //    SqlParameter[] objSqlParameter = new SqlParameter[2];

    //    objSqlParameter[0] = new SqlParameter("@Id", SqlDbType.BigInt);
    //    objSqlParameter[0].Value = Convert.ToInt32(Request.QueryString["bid"]);

    //    objSqlParameter[1] = new SqlParameter("@doctype", SqlDbType.SmallInt);
    //    objSqlParameter[1].Value = Convert.ToInt32(Request.QueryString["type"]);
    //    pgCompanies.SqlParameterList = objSqlParameter;
    //    pgCompanies.StoredProcedureName = "DisplayBlog";
    //}

    // This function use to redirect at community/community.aspx when anyone access page with wrong/change  URL

    private void preventErrorWhenUrlChangesByUser()
    {
        if (Request.QueryString["type"] != null)
        {
            if (Request.QueryString["type"].ToString() != Convert.ToString("2") && Request.QueryString["type"].ToString() != Convert.ToString("4"))
            {
                Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
            }
        }
        if (Request.QueryString["monthid"] == null || Request.QueryString["monthid"].ToString().Length <= 0)
        {
            monthid = true;
        }
        if (Request.QueryString["monthid"] == null && Request.QueryString["type"] == null)
        {
            Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
        }

        if (Request.QueryString["bid"] == null || Request.QueryString["bid"].ToString().Length <= 0)
        {
            bid = true;
        }
        if (monthid == true && bid == true)
        {
            Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");

        }
        //&& Convert.ToBoolean((Convert.ToInt32(Request.QueryString["monthid"]) > Convert.ToInt32("12") || Convert.ToInt32(Request.QueryString["monthid"]) < Convert.ToInt32("1")))        
        if (Request.QueryString["monthid"] != null && Manager.IsNumeric(Request.QueryString["monthid"]) == false)
        {
            Response.Redirect(Session["strHttp"] + "/Community/Community.aspx");
        }
    }

   
}
